package com.example.mybatisplus.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.domain.User;
import com.example.mybatisplus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: springboot-cli-util
 * @author: Wu ZiLin
 * @create: 2023-10-19 09:54
 * @description:
 **/
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询所有用户信息
     */
    @GetMapping("/user")
    public List<User> getUsers() {
        return userService.list();
    }

    /**
     * 查询单个用户信息
     */
    @GetMapping("/user/{id}")
    public User getUser(@PathVariable Long id) {
        return userService.getById(id);
    }
    /**
     * 分页查询
     */
    @GetMapping("/getUserPage")
    public IPage<User> getUserPage(@RequestParam int current,@RequestParam int size){
        Page<User> userPage = new Page<>(current , size);
        return userService.page(userPage);
    }
    /**
     * 按照条件分页查询
     */
    @GetMapping("/getPage")
    public IPage<User> getUserPage2(@RequestParam String nameLike){
        LambdaQueryWrapper<User> userLambdaQueryWrapper = Wrappers.lambdaQuery();
        userLambdaQueryWrapper.like(User::getName , nameLike);
        Page<User> userPage = new Page<>(1 , 5);
        IPage<User> userIPage = userService.page(userPage,userLambdaQueryWrapper);
        return userIPage;
    }
    /**
     * 新增，修改用户信息
     */
    @PostMapping("/user")
    public boolean updateById(@RequestBody User user) {
        return userService.saveOrUpdate(user);
    }

    /**
     * 查询所有用户信息
     */
    @DeleteMapping("/user/{id}")
    public boolean updateById(@PathVariable Long id) {
        return userService.removeById(id);
    }

}
